<?php

/*
 * Author : Andi Kurniawan
 * Follow me : @nakamichikun
 */

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Service extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('modmem');
        $this->load->model('modws');
    }

    function index() {
        $this->load->library('xmlrpc');
        $this->load->library('xmlrpcs');
        $config['functions']['message'] = array('function' => 'Service.message');
        $config['functions']['auth'] = array('function' => 'Service.auth');
        $config['functions']['test'] = array('function' => 'Service.test');
        $config['functions']['pesan'] = array('function' => 'Service.pesan');
        $this->xmlrpcs->initialize($config);
        $this->xmlrpcs->serve();
    }

    function loginFailed() {
        $data = array(
            'code' => '00',
            'message' => 'Login ditolak.'
        );
        return $data;
    }

    function loginSuccess() {
        $data = array(
            'code' => '01',
            'message' => 'Login berhasil.'
        );
        return $data;
    }

    function transaksiDitolak() {
        $data = array(
            'code' => '02',
            'message' => 'Akses ditolak.'
        );
        return $data;
    }

    function transaksiBerhasil() {
        $data = array(
            'code' => '03',
            'message' => 'Akses diijinkan, pengambilan data berhasil.'
        );
        return $data;
    }

    function transaksiKosong() {
        $data = array(
            'code' => '04',
            'message' => 'Akses diijinkan, tidak ada data yang dapat diambil.'
        );
        return $data;
    }
    
    function transaksiDiijinkan(){
        $data = array(
            'code' => '05',
            'message' => 'Akses diijinkan.'
        );
        return $data;
    }

    // Listener from client
    function message($request) {
        $parameters = $request->output_parameters();
        $data = array(
            'nop' => $parameters['0'],
            'nama' => $parameters['1'],
            'telp' => $parameters['2'],
            'tahun' => $parameters['3'],
            'nominal' => $parameters['4'],
            'pesan' => $parameters['5'],
            'user' => $parameters['6']
        );
        $this->modmem->setMessage($data);
        $response = array(array('Berhasil'), 'struct');
        return $this->xmlrpc->send_response($response);
    }

    function auth($request) {
        $parameters = $request->output_parameters();
        $data = array(
            'username' => $parameters['0'],
            'password' => $parameters['1']
        );
        $reply = $this->modmem->authentic($data);
        if ($reply->username != $data['username'] && $reply->password != $data['password']) {
            $response = array(
                'code' => '00',
                'status' => 'Access denied'
            );
            $enc = json_encode($response);
            return $this->xmlrpc->send_response($enc);
        } else {
            $response = array(
                'code' => '01',
                'status' => 'Access Granted.',
            );
            $enc = json_encode($response);
            return $this->xmlrpc->send_response($enc);
        }
    }

    function test($request) {
        $parameters = $request->output_parameters();
        $input = array(
            'username' => $parameters['0'],
            'password' => $parameters['1'],
            'access' => 'test'
        );
        $reply = $this->modmem->login($input);
        if ($reply == TRUE) {
            $role = $this->modmem->role($input);
            if ($role == TRUE) {
                $data = $this->transaksiDiijinkan();
            } else {
                $data = $this->transaksiDitolak();
            }
        } else {
            $data = $this->loginFailed();
        }
        $enc = json_encode($data);
        $rec = array(
            'username' => $parameters['0'],
            'access' => 'test',
            'access_from' => $this->input->ip_address(),
            'data' => $enc
        );
        $this->modws->addLog($rec);
        return $this->xmlrpc->send_response($enc);
    }

    function pesan($request) {
        $parameters = $request->output_parameters();
        $input = array(
            'username' => $parameters['0'],
            'password' => $parameters['1'],
            'access' => 'pesan'
        );
        $reply = $this->modmem->login($input);
        if ($reply == TRUE) {
            $role = $this->modmem->role($input);
            if ($role == TRUE) {
                $data = $this->synPesan();
            } else {
                $data = array($this->transaksiDitolak(), NULL);
            }
        } else {
            $data = array($this->loginFailed(), NULL);
        }
        $enc = json_encode($data);
        $rec = array(
            'username' => $parameters['0'],
            'access' => 'pesan',
            'access_from' => $this->input->ip_address(),
            'data' => $enc
        );
        $this->modws->addLog($rec);
        return $this->xmlrpc->send_response($enc);
    }

    function synPesan() {
        $data = $this->modmem->getPesan();
        if ($data == NULL) {
            return array($this->transaksiKosong(), $data);
        } else {
            foreach ($data as $da) {
                $id = $da->id;
                $this->modmem->updPesan($id);
            }
            return array($this->transaksiBerhasil(), $data);
        }
    }

}

?>